var Sidebar = {
    
    update: function() {
        var title = "<h3>"+Local.sidebar_title+"</h3>\n";
        var text = ""
        summary = Sidebar.summary();
        exams = Sidebar.moduleExams();
        mean = Sidebar.gpa();
        
        if (mean.grade) text += mean.text;
        if (summary.count) text += summary.text;
        if (exams.count >= 5 && exams.thesis) text += Sidebar.bscgrade(mean.credits);
        text += exams.text;
        
        $("summary").innerHTML = title+text;
    },
    
    bscgrade: function(credits) {
        if (credits <180) {
            return "<p>"+Local.sidebar_more_credits.replace("$1",180-credits)+"</p>"
        }
        
        var modGrade = 0;
        for (mod in Globals.modules) {
            if (mod != "trash" && Globals.modules[mod].moduleExam) modGrade += Globals.modules[mod].grade;
        }
        modGrade = (modGrade / 5 * 2 + Globals.courses.ffffffffffffffffffffffffffffffff.grade) / 3;
        modGrade = parseInt(modGrade * 10) / 10;
        
        if (modGrade < 1.3)
            var text = "<p class='hasinfo' onclick='Message.gradeHelp();'>"+Local.sidebar_bsc_grade_dist.replace("$1", grade2str(modGrade))+"</p>"
        else
            var text = "<p class='hasinfo' onclick='Message.gradeHelp();'>"+Local.sidebar_bsc_grade.replace("$1", grade2str(modGrade))+"</p>"
        return text;
        
    },
    
    gpa: function() {
        var grade = 0;
        var credits = 0;
        var thesis = Globals.courses.ffffffffffffffffffffffffffffffff;
        for (hash in Globals.courses) {
            if (Globals.courses[hash].grade && hash != "ffffffffffffffffffffffffffffffff") {
                credits += Globals.courses[hash].credits;
                grade += Globals.courses[hash].credits * Globals.courses[hash].grade;
            }
        }
        
        var mean = 0;
        if (credits>0) mean = grade / credits;
        
        var text = "<p>"+Local.sidebar_gpa.replace("$1", grade2str(mean)).replace("$2", grade2str(5-mean)).replace("$3", credits+(thesis?15:0))+"</p>";
        
        return {text: text, grade: mean, credits: credits+(thesis?15:0)}
 
    },
    
    summary: function() {
        var text = "<p>"+Local.sidebar_open_modules+"</p>\n<ul>\n";
        var open_exams = 0;
        
        for (mod in Globals.modules) {
            if (mod != "trash" && Globals.modules[mod].compCredits < Globals.modules[mod].maxCompCredits) {
                open_exams++;
                var addclass = open_exams % 2 == 0 ? ' alt' : '';
                text += "<li class='hasinfo"+addclass+"' onclick='Message.moduleHelp(\""+mod+"\")'>"+Globals.modules[mod].name+"</li>\n";
            }                
        }
        text += "</ul>";
        return {text: text, count: open_exams};
    },
    
    
    moduleExams: function() {
        var text = "<h3>"+Local.sidebar_module_exams+"</h3>\n<ul>\n"
        var mod_exams = 0;
        var thesis = Globals.courses.ffffffffffffffffffffffffffffffff;
        for (mod in Globals.modules) {
            if (mod != "trash" && Globals.modules[mod].moduleExam) {
                mod_exams++;
                var addclass = mod_exams % 2 == 0 ? ' alt' : '';
                text += "<li class='hasinfo"+addclass+"' onclick='Message.moduleHelp(\""+mod+"\")'><span class='name'>"+Globals.modules[mod].name+"</span><span class='grade'>"+grade2str(Globals.modules[mod].grade)+"</span></li>\n";
            }                
        }
        var addclass = mod_exams % 2 == 1 ? ' alt' : '';
        if (thesis)
            text += "<li class='hasinfo"+addclass+"' onclick='Message.thesisHelp()'><span class='name'>Bachelor's Thesis</span><span class='grade'>"+grade2str(thesis.grade)+"</span></li>\n";
        text += "</ul>";
        
        if (mod_exams<5 && thesis) {
            text += "<p>"+Local.sidebar_more_exams.replace("$1", 5-mod_exams)+"</p>";
        } else if (mod_exams<5) {
            text += "<p>"+Local.sidebar_more_exams_thesis.replace("$1", 5-mod_exams)+"</p>";
        } else if (!thesis) {
            text += "<p>"+Local.sidebar_more_thesis+"</p>";
        }
        
        return {text: text, count: mod_exams, thesis: thesis && thesis.grade > 0};
    }
    
}
